/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditCampaignDlg.java
 *
 * Created on May 8, 2010, 11:34:42 AM
 */
package com.aptech.campaign.ui;

import com.aptech.campaign.biz.CampaignBiz;
import com.aptech.campaign.entities.Product;
import com.aptech.campaign.entities.TypeCampaign;
import com.aptech.campaign.entities.TypeOffer;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.JOptionPane;

/**
 *
 * @author Tai Nguyen
 */
public class EditCampaignDlg extends javax.swing.JDialog {

    CampaignBiz camBuz = new CampaignBiz();
    public static CampaignPnl cam;

    public EditCampaignDlg(CampaignPnl cam, boolean modal) {
        super();
        EditCampaignDlg.cam = cam;
        initComponents();
        setLocationRelativeTo(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    /**
     * @param
     * @return boolean value
     */
    private boolean valid() {
        if (!txfName.getText().equalsIgnoreCase("") && cbbProduct.getSelectedIndex() != 0 && cbbTypecam.getSelectedIndex() != 0
                && dtcEbdDate.getDate() != null && dtcStartDate.getDate() != null) {
            return true;
        }
        return false;
    }

    /**
     * 
     */
    private void saveUpdate() {
        ArrayList<Product> arrPro = camBuz.findByAllProduct();
        ArrayList<TypeCampaign> arrTC = camBuz.findByAllTypeCampaign();
        ArrayList<TypeOffer> arrTO = camBuz.findByAllTypeOffer();
        int camID = Integer.parseInt(txfCampaignID.getText());
        String campaignName = txfName.getText();
        int indexCbbPro = cbbProduct.getSelectedIndex() - 1;
        int productID = arrPro.get(indexCbbPro).getProductID();

        int indexCbbTC = cbbTypecam.getSelectedIndex() - 1;
        int typeID = arrTC.get(indexCbbTC).getTypeID();

        int indexCbbTO = cbbTypeOffer.getSelectedIndex() - 1;
        int offerID;
        if (indexCbbTO >= 0) {
            offerID = arrTO.get(indexCbbTO).getOfferID();
        } else {
            offerID = 0;
        }
        //convert Date calender to date sql
        Calendar ca = Calendar.getInstance();
        ca.setTime(dtcStartDate.getDate());
        java.sql.Date startDate = new java.sql.Date(ca.getTimeInMillis());
        //convert Date calender to date sql
        //Calendar ca = Calendar.getInstance();
        ca.setTime(dtcEbdDate.getDate());
        java.sql.Date endDate = new java.sql.Date(ca.getTimeInMillis());

        if (camBuz.storeCampaign(camID, campaignName, productID, startDate, endDate, typeID, offerID)) {
            JOptionPane.showMessageDialog(this, camBuz.getLasterror());
            dispose();
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnlTitle = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();
        lblCampaignId = new javax.swing.JLabel();
        txfCampaignID = new javax.swing.JTextField();
        lblName = new javax.swing.JLabel();
        txfName = new javax.swing.JTextField();
        lblStartDate = new javax.swing.JLabel();
        dtcStartDate = new com.toedter.calendar.JDateChooser();
        lblEndDate = new javax.swing.JLabel();
        dtcEbdDate = new com.toedter.calendar.JDateChooser();
        lblProduct = new javax.swing.JLabel();
        cbbProduct = new javax.swing.JComboBox();
        lblTypeCam = new javax.swing.JLabel();
        cbbTypecam = new javax.swing.JComboBox();
        cbbTypeOffer = new javax.swing.JComboBox();
        lblTypeOffer = new javax.swing.JLabel();
        btnSavve = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        pnlTitle.setBackground(new java.awt.Color(51, 102, 255));
        pnlTitle.setForeground(new java.awt.Color(51, 102, 255));
        pnlTitle.setMinimumSize(new java.awt.Dimension(400, 20));
        pnlTitle.setLayout(new java.awt.BorderLayout());

        lblTitle.setBackground(new java.awt.Color(51, 102, 255));
        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 20)); // NOI18N
        lblTitle.setForeground(new java.awt.Color(51, 102, 255));
        lblTitle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/editCampaign.jpg"))); // NOI18N
        pnlTitle.add(lblTitle, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 30, 0);
        getContentPane().add(pnlTitle, gridBagConstraints);

        lblCampaignId.setText("CampaignID : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 17, 21, 0);
        getContentPane().add(lblCampaignId, gridBagConstraints);

        txfCampaignID.setColumns(10);
        txfCampaignID.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 53;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 16, 21, 0);
        getContentPane().add(txfCampaignID, gridBagConstraints);

        lblName.setText("Name : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 17, 21, 0);
        getContentPane().add(lblName, gridBagConstraints);

        txfName.setColumns(10);
        txfName.setMaximumSize(new java.awt.Dimension(100, 20));
        txfName.setMinimumSize(new java.awt.Dimension(100, 20));
        txfName.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 53;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 16, 21, 0);
        getContentPane().add(txfName, gridBagConstraints);

        lblStartDate.setText("Start Date : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 17, 21, 0);
        getContentPane().add(lblStartDate, gridBagConstraints);

        dtcStartDate.setMaximumSize(new java.awt.Dimension(100, 20));
        dtcStartDate.setMinimumSize(new java.awt.Dimension(100, 20));
        dtcStartDate.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 64;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 16, 21, 0);
        getContentPane().add(dtcStartDate, gridBagConstraints);

        lblEndDate.setText("End Date : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 17, 21, 0);
        getContentPane().add(lblEndDate, gridBagConstraints);

        dtcEbdDate.setMaximumSize(new java.awt.Dimension(100, 20));
        dtcEbdDate.setMinimumSize(new java.awt.Dimension(100, 20));
        dtcEbdDate.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 64;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 16, 21, 0);
        getContentPane().add(dtcEbdDate, gridBagConstraints);

        lblProduct.setText("Product : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 17, 21, 0);
        getContentPane().add(lblProduct, gridBagConstraints);

        cbbProduct.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Product--" }));
        cbbProduct.setMaximumSize(new java.awt.Dimension(200, 20));
        cbbProduct.setMinimumSize(new java.awt.Dimension(150, 20));
        cbbProduct.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 37;
        gridBagConstraints.ipady = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 16, 21, 0);
        getContentPane().add(cbbProduct, gridBagConstraints);

        lblTypeCam.setText("Type Campaign : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 17, 21, 0);
        getContentPane().add(lblTypeCam, gridBagConstraints);

        cbbTypecam.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Type Campaign--" }));
        cbbTypecam.setMaximumSize(new java.awt.Dimension(150, 20));
        cbbTypecam.setMinimumSize(new java.awt.Dimension(150, 20));
        cbbTypecam.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 40;
        gridBagConstraints.ipady = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 16, 21, 0);
        getContentPane().add(cbbTypecam, gridBagConstraints);

        cbbTypeOffer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Type Offer--" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 40;
        gridBagConstraints.ipady = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 16, 31, 0);
        getContentPane().add(cbbTypeOffer, gridBagConstraints);

        lblTypeOffer.setText("Type Offer : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 17, 31, 0);
        getContentPane().add(lblTypeOffer, gridBagConstraints);

        btnSavve.setBackground(new java.awt.Color(51, 102, 255));
        btnSavve.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnSavve.setForeground(new java.awt.Color(255, 255, 255));
        btnSavve.setText("Save Change");
        btnSavve.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSavveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 13, 0);
        getContentPane().add(btnSavve, gridBagConstraints);

        btnCancel.setBackground(new java.awt.Color(51, 102, 255));
        btnCancel.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnCancel.setForeground(new java.awt.Color(255, 255, 255));
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(0, 27, 13, 0);
        getContentPane().add(btnCancel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnSavveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSavveActionPerformed
        if (!valid()) {
            JOptionPane.showMessageDialog(this, "Please input data Fully!");
        } else if (dtcStartDate.getDate().compareTo(dtcEbdDate.getDate()) >= 0) {
            JOptionPane.showMessageDialog(this, "StartDate and EndDate not valid!");
        } else {
            saveUpdate();
            cam.loadTable();
        }
    }//GEN-LAST:event_btnSavveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                EditCampaignDlg dialog = new EditCampaignDlg(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    public javax.swing.JButton btnSavve;
    public javax.swing.JComboBox cbbProduct;
    public javax.swing.JComboBox cbbTypeOffer;
    public javax.swing.JComboBox cbbTypecam;
    public com.toedter.calendar.JDateChooser dtcEbdDate;
    public com.toedter.calendar.JDateChooser dtcStartDate;
    private javax.swing.JLabel lblCampaignId;
    private javax.swing.JLabel lblEndDate;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblProduct;
    private javax.swing.JLabel lblStartDate;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblTypeCam;
    private javax.swing.JLabel lblTypeOffer;
    private javax.swing.JPanel pnlTitle;
    public javax.swing.JTextField txfCampaignID;
    public javax.swing.JTextField txfName;
    // End of variables declaration//GEN-END:variables
}
